package com.bango.nokia.testapp;

import java.util.Hashtable;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.bango.nokia.java.BangoAgent;

public class NokiaJavaApplicationTestApp extends MIDlet implements
		CommandListener {
	private Form formMain;
	private TextField txtEventName;
	private TextField txtEventDetail;
	private TextField txtEventValue;
	private TextField txtParamName1;
	private TextField txtParamValue1;
	private TextField txtParamName2;
	private TextField txtParamValue2;

	private Command cmdLog = new Command("Log", Command.SCREEN, 1);
	private Command cmdPostNow = new Command("Post Now", Command.SCREEN, 1);
	private Command cmdError = new Command("Error", Command.SCREEN, 1);
	private Command cmdPageView = new Command("P View", Command.SCREEN, 1);
	private Command cmdReset = new Command("Reset", Command.SCREEN, 1);
	private Command cmdClear = new Command("Clear", Command.SCREEN, 1);

	private Command cmdSearch = new Command("Search", Command.SCREEN, 1);
	private Command cmdLiveVideoStart = new Command("LV Start", Command.SCREEN,
			1);
	private Command cmdLiveVideoStop = new Command("LV Stop", Command.SCREEN, 1);
	private Command cmdOnDemandVideoStart = new Command("OD Start",
			Command.SCREEN, 1);
	private Command cmdOnDemandVideoStop = new Command("OD Stop",
			Command.SCREEN, 1);
	private Command cmdIdle = new Command("Idle", Command.SCREEN, 1);
	private Command cmdResume = new Command("Resume", Command.SCREEN, 1);

	private Command cmdViewBatch = new Command("V Batch", Command.SCREEN, 1);
	private Command cmdExit = new Command("Get Out", Command.EXIT, 1);

	public NokiaJavaApplicationTestApp() {

		// Configuration Options here
		
		// Start Session here
		// BangoAgent.onStartSession([Your Application ID]);	

		formMain = new Form("Symbian Test App");
		txtEventName = new TextField("Event Name:", "BB Event", 50,
				TextField.LAYOUT_LEFT);
		txtEventValue = new TextField("Event Value:", "BB Event Value", 50,
				TextField.LAYOUT_LEFT);
		txtEventDetail = new TextField("Event Detail:", "BB Event Detail", 200,
				TextField.LAYOUT_LEFT);
		txtParamName1 = new TextField("Parameter 1:", "BGO_SOURCE", 50,
				TextField.LAYOUT_LEFT);
		txtParamValue1 = new TextField("Param Val 1:", "BB SOURCE", 50,
				TextField.LAYOUT_LEFT);
		txtParamName2 = new TextField("Parameter 2:", "BGO_TERM", 50,
				TextField.LAYOUT_LEFT);
		txtParamValue2 = new TextField("Param Val 2:", "BB TERM", 50,
				TextField.LAYOUT_LEFT);

		formMain.append(txtEventName);
		formMain.append(txtEventValue);
		formMain.append(txtEventDetail);
		formMain.append(txtParamName1);
		formMain.append(txtParamValue1);
		formMain.append(txtParamName2);
		formMain.append(txtParamValue2);

		Reset();

		formMain.addCommand(cmdLog);
		formMain.addCommand(cmdPostNow);
		formMain.addCommand(cmdViewBatch);
		formMain.addCommand(cmdError);
		formMain.addCommand(cmdPageView);
		formMain.addCommand(cmdReset);
		formMain.addCommand(cmdClear);
		formMain.addCommand(cmdSearch);
		formMain.addCommand(cmdLiveVideoStart);
		formMain.addCommand(cmdLiveVideoStop);
		formMain.addCommand(cmdOnDemandVideoStart);
		formMain.addCommand(cmdOnDemandVideoStop);
		formMain.addCommand(cmdIdle);
		formMain.addCommand(cmdResume);
		formMain.addCommand(cmdExit);

		formMain.setCommandListener(this);

	}

	private void Clear() {
		txtEventName.setString("");
		txtEventDetail.setString("");
		txtEventValue.setString("");
		txtParamName1.setString("");
		txtParamValue1.setString("");
		txtParamName2.setString("");
		txtParamValue2.setString("");
	}

	public void commandAction(Command c, Displayable d) {
		if (c == cmdExit) {
			try {
				destroyApp(true);
			} catch (MIDletStateChangeException e) {
				e.printStackTrace();
			}
		}

		if (c == cmdLog) {
			Log();
		}

		if (c == cmdPostNow) {
			PostNow();
		}

		if (c == cmdReset) {
			Reset();
		}

		if (c == cmdClear) {
			Clear();
		}

		if (c == cmdSearch) {
			OnSearch();
		}

		if (c == cmdPageView) {
			PageView();
		}

		if (c == cmdLiveVideoStart) {
			LiveVideoStart();
		}

		if (c == cmdLiveVideoStop) {
			LiveVideoStop();
		}

		if (c == cmdOnDemandVideoStart) {
			OnDemandVideoStart();
		}

		if (c == cmdOnDemandVideoStop) {
			OnDemandVideoStop();
		}

		if (c == cmdIdle) {
			Idle(true);
		}

		if (c == cmdResume) {
			Resume(true);
		}

		if (c == cmdError) {
			Error(true);
		}
		
		if (c == cmdViewBatch){
			viewBatch();
		}
	}

	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
		BangoAgent.onEndSession();
		notifyDestroyed();
	}

	private void Error(boolean showMessage) {
		BangoAgent.onError("Test Error", "Symbian SDK Test Error", "Symbian");
		if (showMessage) {
			ShowMessage("Error Logged");
		}
	}

	private String EventDetail() {
		return txtEventDetail.getString();
	}

	private String EventName() {
		return txtEventName.getString();
	}

	private Hashtable EventParameters() {
		Hashtable eventParameters;
		eventParameters = new Hashtable();

		if (txtParamName1.getString().length() > 0
				|| txtParamName2.getString().length() > 0) {
			if (txtParamName1.getString().length() > 0) {
				eventParameters.put(txtParamName1.getString(),
						txtParamValue1.getString());
			}
			if (txtParamName2.getString().length() > 0) {
				eventParameters.put(txtParamName2.getString(),
						txtParamValue2.getString());
			}
		}

		return eventParameters;
	}

	private String EventValue() {
		return txtEventValue.getString();
	}

	private int getOverload() {
		/*
		 * 1 - Only Event Name Provided
		 * 2 - Event name and value provided
		 * 3 - Event name, value and detail provided
		 * 4 - Event name, value and event parameters provided
		 * 5 - Event name, value, detail and event parameters provided
		 */

		String eventValue = EventValue();
		String eventDetail = EventDetail();
		Hashtable eventParameters = EventParameters();

		if(eventValue == null){ eventValue = "";}
		if(eventDetail == null){ eventDetail = "";}

		int rtnValue = 1;

		if ((eventValue.length() <= 0)) {
			return rtnValue;
		}

		if ((eventDetail.length() <= 0) && eventParameters.isEmpty()) {
			rtnValue = 2;
			return rtnValue;
		}

		if ((eventDetail.length() > 0) && eventParameters.isEmpty()) {
			return 3;
		}

		if ((eventDetail.length() <= 0) && !eventParameters.isEmpty()) {
			return 4;
		}

		if ((eventDetail.length() > 0) && !eventParameters.isEmpty()) {
			rtnValue = 5;
			return rtnValue;
		}

		return rtnValue;
	}

	private void Idle(boolean showMessage) {
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onIdle();
			break;
		case 2:
			BangoAgent.onIdle(EventValue());
			break;
		case 3:
			BangoAgent.onIdle(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onIdle(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onIdle(EventValue(), EventDetail(),
					EventParameters());
			break;
		}
		
		if (showMessage) {
			ShowMessage("onIdle");
		}
	}

	private void LiveVideoStart() {
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onLiveVideoStart();
			break;
		case 2:
			BangoAgent.onLiveVideoStart(EventValue());
			break;
		case 3:
			BangoAgent.onLiveVideoStart(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onLiveVideoStart(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onLiveVideoStart(EventValue(), EventDetail(),
					EventParameters());
			break;
		}

		ShowMessage("onLiveVideoStart");
	}

	private void LiveVideoStop() {
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onLiveVideoStop();
			break;
		case 2:
			BangoAgent.onLiveVideoStop(EventValue());
			break;
		case 3:
			BangoAgent.onLiveVideoStop(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onLiveVideoStop(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onLiveVideoStop(EventValue(), EventDetail(),
					EventParameters());
			break;
		}
		
		ShowMessage("onLiveVideoStop");
	}

	private void Log() {
		
		switch (getOverload()) {
		case 1:
			BangoAgent.onEvent(EventName());
			break;
		case 2:
			BangoAgent.onEvent(EventName(), EventValue());
			break;
		case 3:
			BangoAgent.onEvent(EventName(), EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onEvent(EventName(), EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onEvent(EventName(), EventValue(), EventDetail(),
					EventParameters());
			break;
		}
		
		ShowMessage("Event Logged");
	}
	
	private void OnDemandVideoStart() {
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onDemandVideoStart();
			break;
		case 2:
			BangoAgent.onDemandVideoStart(EventValue());
			break;
		case 3:
			BangoAgent.onDemandVideoStart(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onDemandVideoStart(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onDemandVideoStart(EventValue(), EventDetail(),
					EventParameters());
			break;
		}
		
		ShowMessage("onDemandVideoStart");
	}

	private void OnDemandVideoStop() {
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onDemandVideoStop();
			break;
		case 2:
			BangoAgent.onDemandVideoStop(EventValue());
			break;
		case 3:
			BangoAgent.onDemandVideoStop(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onDemandVideoStop(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onDemandVideoStop(EventValue(), EventDetail(),
					EventParameters());
			break;
		}
		
		ShowMessage("onDemandVideoStop");
	}

	private void OnSearch() {
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onSearch();
			break;
		case 2:
			BangoAgent.onSearch(EventValue());
			break;
		case 3:
			BangoAgent.onSearch(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onSearch(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onSearch(EventValue(), EventDetail(),
					EventParameters());
			break;
		}
		
		ShowMessage("onSearch");
	}

	private void PageView() {
	
		switch (getOverload()) {
		
		case 1:
			BangoAgent.onPageView();
			break;
		case 2:
			BangoAgent.onPageView(EventValue());
			break;
		case 3:
			BangoAgent.onPageView(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onPageView(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onPageView(EventValue(), EventDetail(),
					EventParameters());
			break;
		}
		
		ShowMessage("onPageView");
	}

	protected void pauseApp() {
		Idle(false);
	}

	private void PostNow() {
		BangoAgent.postEvents();
		ShowMessage("Events Posted");
	}

	private void Reset() {
		txtEventName.setString("Symbian Event");
		txtEventDetail.setString("Symbian Event Detail");
		txtEventValue.setString("Symbian Event Value");
		txtParamName1.setString("BGO_SOURCE");
		txtParamValue1.setString("Symbian SOURCE");
		txtParamName2.setString("BGO_TERM");
		txtParamValue2.setString("Symbian TERM");
	}

	private void Resume(boolean showMessage) {
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onResume();
			break;
		case 2:
			BangoAgent.onResume(EventValue());
			break;
		case 3:
			BangoAgent.onResume(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onResume(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onResume(EventValue(), EventDetail(),
					EventParameters());
			break;
		}

		if (showMessage) {
			ShowMessage("onResume");
		}
	}

	private void ShowMessage(String strMessage) {
		Alert alert = new Alert("", strMessage, null, AlertType.INFO);
		alert.setTimeout(1000);
		Display.getDisplay(this).setCurrent(alert, formMain);
	}

	protected void startApp() throws MIDletStateChangeException {
		Display.getDisplay(this).setCurrent(formMain);
	}
	
	private void viewBatch()
	{
		System.out.println(BangoAgent.viewBatch());
	}


}
